<?php
    header('Content-type: text/html; charset=utf-8');
    include('classSimpleImage.php');

    $error = 0;
    $errormessage = '';
    if (isset($_GET['error'])) {
        $error = $_GET['error'];
    }
    if ($error != '0') {
        $error = 1;
        $errormessage = 'test error message';
    }

    $allowedExts = array("jpeg", "jpg", "JPEG", "JPG");
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = end($temp);
    if ((($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg"))
    && ($_FILES["file"]["size"] < 5000000)
    && in_array($extension, $allowedExts)) {
        if ($_FILES["file"]["error"] > 0) {
            $errormessage = $_FILES["file"]["error"] . "<br>";
            $error = 1;
            $result = (object) array('error' => $error, 'errormessage' => $errormessage, 'data' => '');
        } else {
          $date = new DateTime();
          $rnd = $date->format('Y_m_d_H_i_s___');
          $filename = $rnd . $_FILES["file"]["name"];
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "../photos/" . $filename);
          $data = new stdClass;
          $data->filename = $filename;
          $data->size = ($_FILES["file"]["size"] / 1024) . " kB";
          $data->type = $_FILES["file"]["type"];
          $result = (object) array('error' => $error, 'errormessage' => $errormessage, 'data' => $data);

          $image = new SimpleImage();
          $image->load("../photos/" . $filename);
          $image->resizeToHeight(90);
          $image->save("../thumbs/" . $filename);

        }
    } else {
        $errormessage = "Invalid file";
        $error = 1;
        $result = (object) array('error' => $error, 'errormessage' => $errormessage, 'data' => '');
    }
    echo json_encode($result);

?>